package petstore.persistence;

import petstore.persistence.DBUtil;
import petstore.domain.Category;
import petstore.domain.Product;
import petstore.persistence.impl.ProductDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao
{
    private static final String GET_PRODUCTLIST_BY_CATEGORYID = "SELECT productId,categoryId,name,description,price,inform FROM product WHERE categoryId = ?";

    private static final String GET_PRODUCT = "SELECT productId,categoryId,name,description,inform FROM product WHERE productId = ?";

    private static final String GET_PRODUCTLIST = "SELECT productId,categoryId,name,description,price,inform FROM product where name like ?";
    @Override
    public List<Product> getProductListByCategoryId(String categoryId) {
        List<Product> productList = new ArrayList<>();
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_PRODUCTLIST_BY_CATEGORYID);
            preparedStatement.setString(1,categoryId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Product product = new Product();
                product.setProductId(resultSet.getString("productId"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("name"));
                product.setDescription(resultSet.getString("description"));
                product.setPrice(resultSet.getBigDecimal("price"));
                product.setInform(resultSet.getString("inform"));
                //
                System.out.println(resultSet.getString("name")+" "+resultSet.getString("inform"));
                productList.add(product);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return productList;
    }

    @Override
    public Product getProduct(String productId) {
        Product product = null;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_PRODUCT);
            preparedStatement.setString(1,productId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                product = new Product();
                product.setProductId(resultSet.getString("productId"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("name"));
                product.setDescription(resultSet.getString("description"));
                product.setInform(resultSet.getString("inform"));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public List<Product> searchProductList(String name) {
        List<Product> productList = new ArrayList<>();
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_PRODUCTLIST);
            preparedStatement.setString(1,"%" + name + "%");
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Product product = new Product();
                product.setProductId(resultSet.getString("productId"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("name"));
                product.setDescription(resultSet.getString("description"));
                product.setPrice(resultSet.getBigDecimal("price"));
                product.setInform(resultSet.getString("inform"));
                productList.add(product);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return productList;
    }
}
